package com.turing.post.thirdinterface.impl;

import java.io.Serializable;
import java.util.List;

import org.jdom2.Document;
import org.jdom2.Element;

import com.turing.post.thirdinterface.util.ThirdInterfaceApplicationException;
import com.turing.post.thirdinterface.util.ThirdInterfaceRespCode;
import com.turing.post.util.PCPSApplicationException;
import com.turing.post.util.PCPSUtil;

/**
 * 银行应答基类
 * @author ZXL
 *
 */
@SuppressWarnings("serial")
public class PSBCBankRequest implements Serializable {
	/**
	 * 交易返回码
	 */
	public String retCode = "";
	/**
	 * 中文信息
	 */
	public String msg = "";
	/**
	 * 银行流水号
	 */
	public String bankSeq = "";
	/**
	 * 交易完成时间
	 */
	public String retTime = "";
	
	/**
	 * 交易金额
	 */
	public String moneyTrade = "";
	/**
	 * 原交易返回码
	 */
	public String retCodeOrg = "";
	/**
	 * 原中文信息
	 */
	public String msgOrg = "";
	/**
	 * 原银行流水号
	 */
	public String bankSeqOrg = "";
	/**
	 * 原交易完成时间
	 */
	public String retTimeOrg = "";
	/**
	 * 解析XML开卡(销户)应答报文
	 * 
	 * @param doc
	 */
	public static PSBCBankRequest parseOpenOrCancelCardXml(byte[] ibuf) throws PCPSApplicationException {
		Document doc = PSBCBankXmlHelper.getXmlDocumentFromBuffer(ibuf);
		Element root = doc.getRootElement();
		PSBCBankRequest bankrequest = new PSBCBankRequest();
		if (!PCPSUtil.compareObject(root.getName(),
				PSBCBankXmlHelper.XML_NODE_BANKINTF))
			throw new PCPSApplicationException(ThirdInterfaceRespCode.RC_PARAMETER, 
					"invalid XML Root Node Name[" + root.getName()+ "]!");
		
		List<Element> elements = root.getChildren();
		for (Element element: elements){
			if (!PCPSUtil.compareObject(element.getName(),
					PSBCBankXmlHelper.XML_NODE_OUT))
				throw new PCPSApplicationException(ThirdInterfaceRespCode.RC_PARAMETER, 
						"invalid XML Root Node Name[" + element.getName()+ "]!");
			
			bankrequest.retCode = PSBCBankXmlHelper.getMandatoryString(element,
					PSBCBankXmlHelper.XML_NODE_RETCODE);
			bankrequest.msg = PSBCBankXmlHelper.getMandatoryString(element,
					PSBCBankXmlHelper.XML_NODE_MSG);
			bankrequest.bankSeq = PSBCBankXmlHelper.getMandatoryString(element,
					PSBCBankXmlHelper.XML_NODE_BANKSEQ);
			bankrequest.retTime = PSBCBankXmlHelper.getMandatoryString(element,
					PSBCBankXmlHelper.XML_NODE_RETTIME);
		}
		return bankrequest;
	}
	
	/**
	 * 解析XML充值(赎回)应答报文
	 * 
	 * @param doc
	 */
	public static PSBCBankRequest parseDepositOrRedempXml(byte[] ibuf) throws PCPSApplicationException {
		Document doc = PSBCBankXmlHelper.getXmlDocumentFromBuffer(ibuf);
		Element root = doc.getRootElement();
		PSBCBankRequest bankrequest = new PSBCBankRequest();
		if (!PCPSUtil.compareObject(root.getName(),
				PSBCBankXmlHelper.XML_NODE_BANKINTF))
			throw new PCPSApplicationException(ThirdInterfaceRespCode.RC_PARAMETER, 
					"invalid XML Root Node Name[" + root.getName()+ "]!");
		
		List<Element> elements = root.getChildren();
		for (Element element: elements){
			if (!PCPSUtil.compareObject(element.getName(),
					PSBCBankXmlHelper.XML_NODE_OUT))
				throw new PCPSApplicationException(ThirdInterfaceRespCode.RC_PARAMETER, 
						"invalid XML Root Node Name[" + element.getName()+ "]!");
			
			bankrequest.retCode = PSBCBankXmlHelper.getMandatoryString(element,
					PSBCBankXmlHelper.XML_NODE_RETCODE);
			bankrequest.msg = PSBCBankXmlHelper.getMandatoryString(element,
					PSBCBankXmlHelper.XML_NODE_MSG);
			bankrequest.bankSeq = PSBCBankXmlHelper.getMandatoryString(element,
					PSBCBankXmlHelper.XML_NODE_BANKSEQ);
			bankrequest.moneyTrade = PSBCBankXmlHelper.getMandatoryString(element,
					PSBCBankXmlHelper.XML_NODE_MONEYTRADE);
			bankrequest.retTime = PSBCBankXmlHelper.getMandatoryString(element,
					PSBCBankXmlHelper.XML_NODE_RETTIME);
		}
		return bankrequest;
	}
	
	/**
	 * 解析XML对明细账(对总账)应答报文
	 * 
	 * @param doc
	 */
	public static PSBCBankRequest parseReconciliationDetailOrTotalXml(byte[] ibuf) throws PCPSApplicationException {
		Document doc = PSBCBankXmlHelper.getXmlDocumentFromBuffer(ibuf);
		Element root = doc.getRootElement();
		PSBCBankRequest bankrequest = new PSBCBankRequest();
		if (!PCPSUtil.compareObject(root.getName(),
				PSBCBankXmlHelper.XML_NODE_BANKINTF))
			throw new PCPSApplicationException(ThirdInterfaceRespCode.RC_PARAMETER, 
					"invalid XML Root Node Name[" + root.getName()+ "]!");
		
		List<Element> elements = root.getChildren();
		for (Element element: elements){
			if (!PCPSUtil.compareObject(element.getName(),
					PSBCBankXmlHelper.XML_NODE_OUT))
				throw new PCPSApplicationException(ThirdInterfaceRespCode.RC_PARAMETER, 
						"invalid XML Root Node Name[" + element.getName()+ "]!");
			
			bankrequest.retCode = PSBCBankXmlHelper.getMandatoryString(element,
					PSBCBankXmlHelper.XML_NODE_RETCODE);
			bankrequest.msg = PSBCBankXmlHelper.getMandatoryString(element,
					PSBCBankXmlHelper.XML_NODE_MSG);
		}
		return bankrequest;
	}
	
	/**
	 * 解析XML校正交易应答报文
	 * 
	 * @param doc
	 */
	public static PSBCBankRequest parseAdjustBusinessXml(byte[] ibuf) throws PCPSApplicationException {
		Document doc = PSBCBankXmlHelper.getXmlDocumentFromBuffer(ibuf);
		Element root = doc.getRootElement();
		PSBCBankRequest bankrequest = new PSBCBankRequest();
		if (!PCPSUtil.compareObject(root.getName(),
				PSBCBankXmlHelper.XML_NODE_BANKINTF))
			throw new PCPSApplicationException(ThirdInterfaceRespCode.RC_PARAMETER, 
					"invalid XML Root Node Name[" + root.getName()+ "]!");
		
		List<Element> elements = root.getChildren();
		for (Element element: elements){
			if (!PCPSUtil.compareObject(element.getName(),
					PSBCBankXmlHelper.XML_NODE_OUT))
				throw new PCPSApplicationException(ThirdInterfaceRespCode.RC_PARAMETER, 
						"invalid XML Root Node Name[" + element.getName()+ "]!");
			
			bankrequest.retCode = PSBCBankXmlHelper.getMandatoryString(element,
					PSBCBankXmlHelper.XML_NODE_RETCODE);
			bankrequest.msg = PSBCBankXmlHelper.getMandatoryString(element,
					PSBCBankXmlHelper.XML_NODE_MSG);
			bankrequest.retCodeOrg = PSBCBankXmlHelper.getMandatoryString(element,
					PSBCBankXmlHelper.XML_RET_CODE_ORG);
			bankrequest.msgOrg = PSBCBankXmlHelper.getMandatoryString(element,
					PSBCBankXmlHelper.XML_MSG_ORG);
			bankrequest.bankSeqOrg = PSBCBankXmlHelper.getMandatoryString(element,
					PSBCBankXmlHelper.XML_BANK_SEQ_ORG);
			bankrequest.retTimeOrg = PSBCBankXmlHelper.getMandatoryString(element,
					PSBCBankXmlHelper.XML_RET_TIME_ORG);
		}
		return bankrequest;
	}
	
	public static PSBCBankRequest checkRequestBusinessType(String tradeId,byte[] ibuf) throws PCPSApplicationException {
		//验证参数
		if (ibuf == null || ibuf.length <= 0){
			throw new ThirdInterfaceApplicationException(
					ThirdInterfaceApplicationException.ERRMSG_NULLPOINTER,
					ThirdInterfaceRespCode.RC_ARGS_NULL, "参数为空!");
		}
		//验证业务类型
		if (PCPSUtil.isEmpty(tradeId)){
			throw new ThirdInterfaceApplicationException(
					ThirdInterfaceApplicationException.ERRMSG_NULLPOINTER,
					ThirdInterfaceRespCode.RC_ARGS_NULL, "交易码为空!");
		}
		if (PSBCBankXmlHelper.XML_OPENCARD.equals(tradeId) 
				|| PSBCBankXmlHelper.XML_CANCELCARD.equals(tradeId)){
			return parseOpenOrCancelCardXml(ibuf);
		}
		if (PSBCBankXmlHelper.XML_DEPOSIT.equals(tradeId) 
				|| PSBCBankXmlHelper.XML_REDEMP.equals(tradeId)){
			return parseDepositOrRedempXml(ibuf);
		}
		if (PSBCBankXmlHelper.XML_RECONCILIATIONDETAIL.equals(tradeId) 
				|| PSBCBankXmlHelper.XML_RECONCILIATIONTOTAL.equals(tradeId)){
			return parseReconciliationDetailOrTotalXml(ibuf);
		}
		if (PSBCBankXmlHelper.XML_ADJUSTBUSINESS.equals(tradeId)){
			return parseAdjustBusinessXml(ibuf);
		}
		return null;
	}
}
